using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using Husb.Util;
using Husb.Util.Enum;
using CentralOffice.Admin.Data;
using CentralOffice.Admin.BusinessActions;

using AttendHolidayTable = CentralOffice.Admin.Data.AttendDataSet.Attend_HolidayDataTable;
using AttendHolidayRow = CentralOffice.Admin.Data.AttendDataSet.Attend_HolidayRow;

namespace CentralOffice.WebClient.Admin.PublicAffairs.attend
{
    public partial class holiday : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    string str_Id = Request.QueryString["id"].Trim().Trim();
                    if (string.IsNullOrEmpty(str_Id) == false)
                    {
                        AttendHolidayRow holidayRow = null;
                        holidayRow = CentralOffice.Admin.BusinessActions.AttendHoliday.GetById(Int32.Parse(str_Id));
                        txtName.Value = holidayRow.IsHolidayNameNull() ? "" : holidayRow.HolidayName;
                        txtBegin.Value = holidayRow.BeginDate.ToString("yyyy-MM-dd");
                        txtEnd.Value = holidayRow.EndDate.ToString("yyyy-MM-dd");
                        txtId.Text = str_Id;
                    }
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SaveHoliday();
            txtId.Text = "";
            Response.Write("<script language=javascript>window.location.href='holiday.aspx';</script>");
        }
        
        protected void odsHolidayConfig_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            int id = (int)e.InputParameters["HolidayId"];
            e.InputParameters.Remove("HolidayId");
            e.InputParameters.Add("id", id);            
        }

        private void SaveHoliday()
        {
            string str_Id = txtId.Text.Trim();
            //String savePath = SiteUtil.GetDocumentFilePath();
            AttendDataSet holiday = GetFields(str_Id);
            int cnt = AttendHoliday.Update(holiday);
        }

        private AttendDataSet GetFields(string id)
        {
            AttendHolidayRow holidayrow = null;

            if (String.IsNullOrEmpty(id))
            {
                holidayrow = (new AttendDataSet()).Attend_Holiday.NewAttend_HolidayRow();
            }
            else
            {
                // update
                holidayrow = CentralOffice.Admin.BusinessActions.AttendHoliday.GetById(Int32.Parse(id));
            }
            holidayrow.HolidayName = txtName.Value.Trim();
            holidayrow.BeginDate = Convert.ToDateTime(txtBegin.Value.Trim());
            holidayrow.EndDate = Convert.ToDateTime(txtEnd.Value.Trim());

            if (String.IsNullOrEmpty(id))
            {
                holidayrow.Table.Rows.Add(holidayrow);
            }

            return holidayrow.Table.DataSet as AttendDataSet;
        }

    }
}
